package com.bdqn;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Sjlx1 {
    public static void main(String[] args) {
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db=dbf.newDocumentBuilder();
            Document doc=db.parse("ch02/Practical/手机收藏信息.xml");
            NodeList list=doc.getElementsByTagName("Brand");
            for (int i=0;i<list.getLength();i++){
                Node node=list.item(i);
                Element element=(Element)node;
                String value=element.getAttribute("name");
                NodeList nodeList=element.getChildNodes();
                for (int j =0;j<nodeList.getLength();j++){
                    Node nod=nodeList.item(j);
                    if (nod.getNodeType()==Node.ELEMENT_NODE){
                        Element el=(Element) nod;
                        String elvalue=el.getAttribute("name");
                        System.out.println("手机品牌:"+value+"\t手机型号:"+elvalue);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
